home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 17 / Computer Interactive cdrom 17 - gen 99.iso / ZDNETIT / CONTENT / SMTPCEMS.ZIP / BCAST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-15  |  4.8 KB  |  174 lines

  1. /*
  2. **  BCAST.C [edit EMAIL.H before compiling]
  3. **
  4. **  This is a WIN32 console mode program that sends
  5. **  the same email to a list of email addresses read
  6. **  from a file of addresses. 
  7. **
  8. **  Also see the USER example program, which verifies
  9. **  email receipients.
  10. */
  11.  
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #include "see.h"
  18. #include "email.h"
  19.  
  20. // Email the file BCAST.MAI to each address
  21. // as listed in the file BCAST.EML
  22.  
  23. #define EMAIL_ADDR_LIST "BCAST.EML"
  24. #define EMAIL_SUBJECT   "Advisory"
  25. #define EMAIL_FILENAME  "@BCAST.MAI"
  26.  
  27. #define MAX_BUF 512
  28. static char Buffer[MAX_BUF];
  29.  
  30. void ShowError(int Code)
  31. {seeErrorText(Code,(LPSTR)Buffer,512);
  32.  printf("%s\n", Buffer);
  33. }
  34.  
  35. void ErrorExit(int Code)
  36. {ShowError(Code);
  37.  exit(1);
  38. }
  39.  
  40. /* read text line (ending with LF) from disk */
  41.  
  42. static int ReadTextLine(int Handle, LPSTR Buffer, int BufLen)
  43. {char Ch;
  44.  int  i;
  45.  int  Size = 0;
  46.  static char TextBuffer[128];
  47.  static int  TextLeft = 0;
  48.  static int  TextRight = 0;
  49.  while(1)
  50.    {/* is TextBuffer[] empty ? */
  51.     if(TextLeft>=TextRight)
  52.       {/* read from disk */
  53.        TextLeft = 0;
  54.        TextRight = _lread(Handle,(LPSTR)TextBuffer,125) - 1;
  55.        if(TextRight<0) return -1;
  56.       }
  57.     /* copy till LF or end of buffer */
  58.     for(i=TextLeft;i<=TextRight;i++)
  59.       {Ch = TextBuffer[i];
  60.        /* throw away CRs */
  61.        if(Ch!='\r') 
  62.          {if((Ch=='\n')||(Size==BufLen-1))
  63.             {/* found LF or buffer is full */
  64.              Buffer[Size] = '\0';
  65.              TextLeft = i + 1;
  66.              return Size;
  67.             }
  68.           else
  69.             {/* save char */
  70.              Buffer[Size++] = 0x7f & Ch;
  71.             }
  72.          }
  73.       }
  74.     /* used up all of TextBuffer[] */
  75.     TextLeft = TextRight;
  76.    } /* end-while */
  77. }
  78.  
  79.  
  80. /*** main ***/
  81.  
  82. void main(void)
  83. {int i, n;
  84.  int Code; 
  85.  int RcptHandle;
  86.  static char FileName[65];
  87.  
  88.  /* open list of addresses file */
  89.  RcptHandle = _lopen((LPSTR)EMAIL_ADDR_LIST,OF_READ|OF_SHARE_DENY_WRITE);
  90.  if(RcptHandle<0)
  91.    {printf("ERROR: Cannot open %s",EMAIL_ADDR_LIST);
  92.     exit(1);
  93.    }
  94.   
  95.  /* display parameters */
  96.  printf("Server : %s\n",(LPSTR)SMTP_HOST_NAME);
  97.  printf("  From : %s\n",(LPSTR)YOUR_EMAIL_ADDR);
  98.  printf("  Subj : %s\n",(LPSTR)EMAIL_SUBJECT);
  99.  printf("  List : %s\n",(LPSTR)EMAIL_ADDR_LIST); 
  100.  printf("  File : %s\n",(LPSTR)EMAIL_FILENAME); 
  101.  
  102.  Code = seeVerifyFormat((LPSTR)YOUR_EMAIL_ADDR);
  103.  if(Code<0) 
  104.    {printf("%s: ",(LPSTR)YOUR_EMAIL_ADDR );
  105.     ErrorExit(Code);
  106.    }
  107.  
  108.  /* define diagnostics log file */
  109.  ///seeStringParam(SEE_LOG_FILE, (LPSTR)"bcast.log");  
  110.  
  111.  printf("Connecting to %s...\n",(LPSTR)SMTP_HOST_NAME);
  112.  Code = seeSmtpConnect(
  113.     (LPSTR)SMTP_HOST_NAME,                     /* SMTP server */
  114.     (LPSTR)YOUR_EMAIL_ADDR,                    /* our return email address */
  115.     (LPSTR)NULL);                              /* Reply-To header */
  116.  if(Code<0) ErrorExit(1);
  117.  
  118.  /* wait up to 60 seconds for server response */
  119.  seeIntegerParam(SEE_MAX_RESPONSE_WAIT, 60000);
  120.  
  121.  /* read each email address in turn */
  122.  for(i=1;;i++)
  123.    {/* read next email destination address */
  124.     n = ReadTextLine(RcptHandle, (LPSTR)Buffer, MAX_BUF);   
  125.     if(n<=0) break;
  126.     /* enclose in <> brackets if necessary */
  127.     if(strchr(Buffer,'<')) strncpy(FileName, Buffer, 64);
  128.     else
  129.       {/* enclose in <> brackets */
  130.        strcpy(FileName,"<");
  131.        strcat(FileName,Buffer);
  132.        strcat(FileName,">");
  133.       }
  134.     /* check email address */
  135.     Code = seeVerifyFormat((LPSTR)FileName);
  136.     if(Code>=0)
  137.       {printf("%3d: Mailing to '%s'...", i, FileName);    
  138.        /* send next email  */
  139.        Code = seeSendEmail(
  140.          (LPSTR)FileName,                 /* To list */
  141.          (LPSTR)NULL,                     /* CC list */
  142.          (LPSTR)NULL,                     /* BCC list */
  143.          (LPSTR)EMAIL_SUBJECT,            /* subject */
  144.          (LPSTR)EMAIL_FILENAME,           /* email filename */
  145.          (LPSTR)NULL);                    /* no attachment */   
  146.        if(Code>=0) printf("OK\n");             
  147.        else 
  148.          {ShowError(Code);
  149.           seeClose();
  150.           printf("FAILS.\n"); 
  151.           /* re-connect */
  152.           printf("Re-connecting to %s...\n",(LPSTR)SMTP_HOST_NAME);
  153.           Code = seeSmtpConnect(
  154.             (LPSTR)SMTP_HOST_NAME,        /* SMTP server */
  155.             (LPSTR)YOUR_EMAIL_ADDR,       /* our return email address */
  156.             (LPSTR)NULL);                 /* Reply-To header */
  157.           if(Code<0) ErrorExit(1);
  158.          }
  159.       }
  160.    else
  161.       {/* display error message */ 
  162.        printf("%3d: ",i);
  163.        seeErrorText(Code,(LPSTR)Buffer,MAX_BUF);
  164.        printf("%s in '%s'\n", Buffer, FileName);
  165.       }
  166.    } /* end-for */
  167.      
  168.  puts("All email sent."); 
  169.  _lclose(RcptHandle);
  170.  seeClose(); 
  171. } /* end main */
  172.  
  173.  
  174.